package cn.iocoder.yudao.module.erp.controller.admin.producthtosrelation;

import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.erp.controller.admin.producthtosrelation.vo.*;
import cn.iocoder.yudao.module.erp.dal.dataobject.producthtosrelation.ProductHtosRelationDO;
import cn.iocoder.yudao.module.erp.service.producthtosrelation.ProductHtosRelationService;

@Tag(name = "管理后台 - ERP 产品对照")
@RestController
@RequestMapping("/erp/product-htos-relation")
@Validated
public class ProductHtosRelationController {

    @Resource
    private ProductHtosRelationService productHtosRelationService;

    @PostMapping("/create")
    @Operation(summary = "创建ERP 产品对照")
    @PreAuthorize("@ss.hasPermission('erp:product-htos-relation:create')")
    public CommonResult<Integer> createProductHtosRelation(@Valid @RequestBody ProductHtosRelationSaveReqVO createReqVO) {
        return success(productHtosRelationService.createProductHtosRelation(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新ERP 产品对照")
    @PreAuthorize("@ss.hasPermission('erp:product-htos-relation:update')")
    public CommonResult<Boolean> updateProductHtosRelation(@Valid @RequestBody ProductHtosRelationSaveReqVO updateReqVO) {
        productHtosRelationService.updateProductHtosRelation(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除ERP 产品对照")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('erp:product-htos-relation:delete')")
    public CommonResult<Boolean> deleteProductHtosRelation(@RequestParam("id") Integer id) {
        productHtosRelationService.deleteProductHtosRelation(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得ERP 产品对照")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('erp:product-htos-relation:query')")
    public CommonResult<ProductHtosRelationRespVO> getProductHtosRelation(@RequestParam("id") Integer id) {
        ProductHtosRelationDO productHtosRelation = productHtosRelationService.getProductHtosRelation(id);
        return success(BeanUtils.toBean(productHtosRelation, ProductHtosRelationRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得ERP 产品对照分页")
    @PreAuthorize("@ss.hasPermission('erp:product-htos-relation:query')")
    public CommonResult<PageResult<ProductHtosRelationRespVO>> getProductHtosRelationPage(@Valid ProductHtosRelationPageReqVO pageReqVO) {
        PageResult<ProductHtosRelationDO> pageResult = productHtosRelationService.getProductHtosRelationPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ProductHtosRelationRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出ERP 产品对照 Excel")
    @PreAuthorize("@ss.hasPermission('erp:product-htos-relation:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportProductHtosRelationExcel(@Valid ProductHtosRelationPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ProductHtosRelationDO> list = productHtosRelationService.getProductHtosRelationPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "ERP 产品对照.xls", "数据", ProductHtosRelationRespVO.class,
                        BeanUtils.toBean(list, ProductHtosRelationRespVO.class));
    }

}